<?php
include("include/config.inc.php");
$pageName = 'planingList';
if($_SESSION['s_userType'] == 'User')
{
   $authorization = "NoAccess";
   foreach($selUsermenuArr as $value)
   {
     if(in_array($pageName, $value))
       {
         $authorization ="Access";
         break;
       }
       else 
       {
        $authorization = "NoAccess";    
       }
   }
   if($authorization == "NoAccess")
   {
     header("location:index.php");
   }
}
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:index.php");
}
else
{    
  $i = 0;
  $isEdit            = 0;
  $partyId           = isset($_REQUEST['partyId']) ? $_REQUEST['partyId'] : 0;
  $temp1             = 0; 
  $temp2             = 0; 
  $temp3             = 0; 
  $entryArray        = array();
  $incomingRegister  = array();
  if(isset($_POST['submitbtn']))
  {
    $planningId =  isset($_REQUEST['planningId']) ? $_REQUEST['planningId'] : 0;
    $incomingId =  isset($_REQUEST['incomingId']) ? $_REQUEST['incomingId'] : 0;
	  $temp1      =  (isset($_POST['temp1'])) && ($_POST['temp1'] != "") ? $_POST['temp1'] : 0 ;
	  $temp2      =  (isset($_POST['temp2'])) && ($_POST['temp2'] != "") ? $_POST['temp2'] : 0 ;
	  $temp3      =  (isset($_POST['temp3'])) && ($_POST['temp3'] != "") ? $_POST['temp3'] : 0 ;
    $soaking1   =  (isset($_POST['soaking1'])) && ($_POST['soaking1'] != "") ? $_POST['soaking1'] : 0 ;
    $soaking2   =  (isset($_POST['soaking2'])) && ($_POST['soaking2'] != "") ? $_POST['soaking2'] : 0 ;
    $soaking3   =  (isset($_POST['soaking3'])) && ($_POST['soaking3'] != "") ? $_POST['soaking3'] : 0 ;
    $fId1       =  (isset($_POST['furnace1'])) && ($_POST['furnace1'] != 0) ? $_POST['furnace1'] : 0 ;
    $fId2       =  (isset($_POST['furnace2'])) && ($_POST['furnace2'] != 0) ? $_POST['furnace2'] : 0 ;
    $fId3       =  (isset($_POST['furnace3'])) && ($_POST['furnace3'] != 0) ? $_POST['furnace3'] : 0 ;
    
  	if($_POST['process3Id'] > 0){
  	  $noOfProcess = 3;
  	}
  	else if($_POST['process2Id'] > 0){
  	  $noOfProcess = 2;
  	}
  	else if($_POST['process1Id'] > 0){
  	  $noOfProcess = 1;
  	}

    if(isset($_POST['planningId']) && $_POST['planningId'] > 0)
    {
    	if($_POST['oldQuantity']!= $_POST['plannedQuantity'] || $_POST['oldWeight'] != $_POST['plannedWeight'])
    	{
    	  $insertIntoPlanning = "INSERT INTO planning(incomingId,partyId,date,quantity,weight,processId1,processId2,processId3,noOfProcess,
    	                                              temp1,soaking1,fId1,temp2,soaking2,fId2,temp3,soaking3,fId3,status)
    		                       VALUES (".$_POST['pIncomingId'].", ".$_POST['pPartyId'].", '".date("Y-m-d")."', ".$_POST['plannedQuantity'].",
    		                               ".$_POST['plannedWeight'].", ".$_POST['process1Id'].", ".$_POST['process2Id'].", ".$_POST['process3Id'].",
    		                               '".$noOfProcess."', '".$temp1."', '".$soaking1."', '".$fId1."', '".$temp2."', '".$soaking2."', '".$fId2."',
    		                               '".$temp3."', '".$soaking3."', '".$fId3."', 'inlogsheet')";
        $insertIntoPlanningRes = mysql_query($insertIntoPlanning);
        if($insertIntoPlanningRes)
        {
        	$updateMainPlanningRecord = "UPDATE planning
        	                                SET quantity = ".($_POST['oldQuantity']-$_POST['plannedQuantity']).",
        	                                      weight = ".($_POST['oldWeight']-$_POST['plannedWeight'])."
        	                              WHERE planningId = ".$_POST['planningId'];
          $updateRes = mysql_query($updateMainPlanningRecord);
        }
        else
        {
        	die("Record Not Inserted : ".$updateMainPlanningRecord."<br>".mysql_error());
        }
    	}
    	else
    	{
    		$updatePlanning = "UPDATE planning
    		                      SET temp1    = '".$temp1."',
                                  soaking1 = '".$soaking1."',
	                                fId1     = '".$fId1."',
	                                temp2    = '".$temp2."',
	                                soaking2 = '".$soaking2."',
	                                fId2     = '".$fId2."',
	                                temp3    = '".$temp3."',
	                                soaking3 = '".$soaking3."',
	                                fId3     = '".$fId3."',
	                                status   = 'inlogsheet'
    		                    WHERE planningId = ".$_POST['planningId'];
    		$updatePlanningRes = mysql_query($updatePlanning);
    		if(!$updatePlanningRes)
    		{
    			die("Update Query Not Inserted : ".$updatePlanning."<br>".mysql_error());
    		}
    	}
    }
    //header("Location:planingList.php");
    //exit();
  }
  
//-------------------party----------------------
  $catTitleArray = array();
  $k = 0;
  $select = "SELECT partyId,partyName
               FROM party
              WHERE partyId IN (SELECT incoming.partyId 
                                  FROM incoming
                                  JOIN planning ON incoming.incomingId = planning.incomingId 
                                 WHERE incoming.status != 'outgoing' 
                                   AND incoming.status != 'outgoingDone'
                                   AND planning.status != 'log complete')
              ORDER BY partyName";
  $selectResult = mysql_query($select);
  while($row = mysql_fetch_array($selectResult))
  {
    $catTitleArray['partyId'][$k]   = $row['partyId'];
    $catTitleArray['partyName'][$k] = $row['partyName'];
    $k++;
  }
  $selectincomingRegister = "SELECT planning.planningId,planning.incomingId,planning.partyId,party.partyName,
                                    DATE_FORMAT(incoming.IncomingDate,'%d-%m-%Y')AS IncomingDate,
                                    planning.quantity,challan,planning.weight,
                                    processName1.processName AS processName1,processName2.processName AS 
                                    processName2,processName3.processName AS processName3,materialName,hardne
                               FROM planning
                               LEFT JOIN incoming ON incoming.incomingId = planning.incomingId
                               LEFT JOIN party ON party.partyId = planning.partyId
                               LEFT JOIN process AS processName1 ON processName1.processId = planning.processId1
                               LEFT JOIN process AS processName2 ON processName2.processId = planning.processId2
                               LEFT JOIN process AS processName3 ON processName3.processId = planning.processId3
                               LEFT JOIN material ON material.materialId = incoming.materialId
                              WHERE (planning.status = 'inlogsheet'
                                 OR planning.status = 'inplanning') ";
  if(isset($_POST['partyId']) && $_POST['partyId'] > 0)                            
  {
	  $selectincomingRegister .= " AND incoming.partyId = ".$_POST['partyId'];
  }
  $selectincomingRegisterRes = mysql_query($selectincomingRegister);
  while($incomingRow = mysql_fetch_array($selectincomingRegisterRes))
  {
	  $incomingRegister[$i]['planningId']   = $incomingRow['planningId'];
	  $incomingRegister[$i]['incomingId']   = $incomingRow['incomingId'];
	  $incomingRegister[$i]['partyName']    = $incomingRow['partyName'];
	  $incomingRegister[$i]['IncomingDate'] = $incomingRow['IncomingDate'];
	  $incomingRegister[$i]['qty']          = $incomingRow['quantity'];
	  $incomingRegister[$i]['challan']      = $incomingRow['challan'];
	  $incomingRegister[$i]['weight']       = $incomingRow['weight'];
	  $incomingRegister[$i]['processName1'] = $incomingRow['processName1'];
	  $incomingRegister[$i]['processName2'] = $incomingRow['processName2'];
	  $incomingRegister[$i]['processName3'] = $incomingRow['processName3'];
	  $incomingRegister[$i]['material']     = $incomingRow['materialName'];
	  $incomingRegister[$i]['hardne']       = $incomingRow['hardne'];
	  $i++;
  }
	$a = 0;
	$furnaceArray = array();
	$selectFurnace = "SELECT fId,furnaceName
	                    FROM furnace
	                   ORDER BY furnaceName";
	$selectFurnaceResult = mysql_query($selectFurnace);
	while($row = mysql_fetch_array($selectFurnaceResult))
	{
	  $furnaceArray['fId'][$a]   = $row['fId'];
	  $furnaceArray['furnaceName'][$a] = $row['furnaceName'];
	  $a++;
	}

  include("./bottom.php");
  $smarty->assign("catTitleArray",$catTitleArray);
	$smarty->assign("furnaceArray",$furnaceArray);
  $smarty->assign("partyId",$partyId);
  $smarty->assign("temp1",$temp1);
  $smarty->assign("temp2",$temp2);
  $smarty->assign("temp3",$temp3);
  $smarty->assign("incomingRegister",$incomingRegister);
  $smarty->assign("msg",'<font size="6">No Record Found</font>');
  $smarty->display('planingList.tpl');
}
?>